package server.feedbacks;

import java.sql.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import json.JSONObjectWrapper;
import server.database.DatabaseColumn;
import server.database.DatabaseService;
import server.database.query.IQueryResult;
import server.servlet.AbstractEntityServlet;
import server.users.UsersTable;

public class FeedbackServlet extends AbstractEntityServlet
{
	private static final long serialVersionUID = -8593919074205421218L;

	public static final String PATH = API + "/feedback";

	public static final String FEEDBACK = "feedback";
	public static final String FEEDBACKS = "feedbacks";

	public static final String OPEN = "0";
	public static final String CLOSED = "1";
	public static final String REVIEWING = "2";

	public static final String NOT_READ = "0";
	public static final String READ = "1";

	@Override
	protected String getEntityName()
	{
		return FEEDBACK;
	}

	@Override
	protected String getEntitiesName()
	{
		return FEEDBACKS;
	}

	@Override
	protected FeedbacksTable getTable()
	{
		return (FeedbacksTable) DatabaseService.getInstance().getTable(FeedbacksTable.TABLE_NAME);
	}

	@Override
	protected void doAddPostSpecific(JSONObjectWrapper request, Map<DatabaseColumn, String> mpValues)
	{
		mpValues.put(FeedbacksTable.SUBMIT_DATE, String.valueOf(new Date(System.currentTimeMillis())));
		mpValues.put(FeedbacksTable.STATUS, OPEN);
		mpValues.put(FeedbacksTable.RESPONSE_READ, NOT_READ);
	}

	protected void doUpdatePostSpecific(JSONObjectWrapper request, Map<DatabaseColumn, String> mpValues)
	{
		if (!mpValues.containsKey(FeedbacksTable.RESPONSE_READ))
		{
			mpValues.put(FeedbacksTable.RESPONSE_READ, NOT_READ);
		}
	}

	protected IQueryResult querySpecificID(HttpServletRequest request, List<DatabaseColumn> lstFields, List<String> lstValues)
	{
		String sUserID = request.getParameter(getTable().ID.getName());
		lstFields.add(FeedbacksTable.USER);
		lstValues.add(sUserID);

		return getTable().getAllEntities(lstFields, lstValues, UsersTable.AND);
	}
}
